package com.ruyuan.seckilling.inventory.listener;

import com.alibaba.fastjson.JSON;
import com.ruyuan.seckilling.common.QueueKey;
import com.ruyuan.seckilling.common.message.OrderCancelMessage;
import com.ruyuan.seckilling.common.message.OrderPayMessage;
import com.ruyuan.seckilling.inventory.service.InventoryService;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author zhonghuashishan
 */
@Slf4j
@Component
@RocketMQMessageListener(topic = QueueKey.QUEUE_CANCEL_ORDER, consumerGroup = "cancelOrderGroup")
public class CancelOrderListener implements RocketMQListener<String> {

    @Autowired
    private InventoryService inventoryService;

    @Override
    public void onMessage(String messageString) {
        log.info("收到订单取消的消息，mesasge={}", messageString);

        OrderCancelMessage message = JSON.parseObject(messageString, OrderCancelMessage.class);

        inventoryService.releaseStock(message.getSequence(), message.getActivityId(), message.getSkuId());
        log.info("释放掉取消订单对应的商品库存");
    }
}
